package com.pet.serve.controller;

import com.pet.serve.entity.Goods;
import com.pet.serve.response.ApiResponse;
import com.pet.serve.service.GoodsService;
import com.pet.serve.repository.GoodsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.data.domain.Page;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/goods")
public class GoodsController {

    private final GoodsService goodsService;
    private final GoodsRepository goodsRepository;

    @Autowired
    public GoodsController(GoodsService goodsService, GoodsRepository goodsRepository) {
        this.goodsService = goodsService;
        this.goodsRepository = goodsRepository;
    }

    // 获取商品数据的通用方法
    @GetMapping("/get")
    public ResponseEntity<ApiResponse> getGoods(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) BigDecimal small,
            @RequestParam(required = false) BigDecimal big,
            @RequestParam("pageNo") int pageNo,
            @RequestParam("pageSize") int pageSize){

        Page<Goods> goodsPage;
        Pageable pageable = PageRequest.of(pageNo, pageSize);
        // 根据参数条件查询商品
        if (small != null && big != null) {
            goodsPage = goodsService.getGoodsByPriceRange(small, big, pageable);
        } else if (name != null && !name.isEmpty()) {
            goodsPage = goodsService.searchGoodsByName(name, pageable);
        } else if (type != null && !type.isEmpty()) {
            goodsPage = goodsService.getGoodsByType(type, pageable);
        } else if (status!=null &&!status.isEmpty()) {
            goodsPage = goodsService.getGoodsByStatus(status, pageable);
        } else {
            goodsPage = goodsService.getAllGoods(pageable);
        }

        ApiResponse response = new ApiResponse(true,"Goods retrieved successfully", goodsPage.getContent(), goodsPage.getTotalElements());
        return ResponseEntity.ok(response);
    }

    // 根据价格区间查询商品
    @GetMapping("/byPriceRange")
    public ResponseEntity<Page<Goods>> getGoodsByPriceRange(@RequestParam BigDecimal minPrice, @RequestParam BigDecimal maxPrice, Pageable pageable) {
        Page<Goods> goods = goodsService.getGoodsByPriceRange(minPrice, maxPrice, pageable);
        return ResponseEntity.ok(goods);
    }

    // 根据名称模糊查询商品
    @GetMapping("/search")
    public ResponseEntity<Page<Goods>> searchGoodsByName(@RequestParam String name, Pageable pageable) {
        Page<Goods> goods = goodsService.searchGoodsByName(name, pageable);
        return ResponseEntity.ok(goods);
    }

    // 根据类型查询商品
    @GetMapping("/byType")
    public ResponseEntity<Page<Goods>> getGoodsByType(@RequestParam String type, Pageable pageable) {
        Page<Goods> goods = goodsService.getGoodsByType(type, pageable);
        return ResponseEntity.ok(goods);
    }


    @DeleteMapping("/delete")
    public ResponseEntity<ApiResponse> deleteGoods(@RequestBody List<Long> goodsIds) {
        for (Long goodsId : goodsIds) {
            boolean result = goodsService.deleteGoodsById(goodsId);
            if (!result) {
                return ResponseEntity.badRequest().body(new ApiResponse(false, "Goods not found", null, 0));
            }
        }
        return ResponseEntity.ok(new ApiResponse(true, "Goods deleted successfully", null, 0));
    }

    // 更新商品状态
    @PutMapping("/{goodId}/status")
    public ResponseEntity<Void> updateGoodsStatus(@PathVariable Long goodId, @RequestParam String status) {
        goodsService.updateGoodsStatus(goodId, status);
        return ResponseEntity.noContent().build();
    }

    @PostMapping("/add")
    public ApiResponse addGoods(@RequestBody Goods goods) {
        try {
            if (goods.getStatus() == null || goods.getStatus().isEmpty()) {
                goods.setStatus("saling");
            }
            goodsRepository.save(goods);
            return new ApiResponse(true, "商品添加成功");
        } catch (Exception e) {
            return new ApiResponse(false, "添加商品失败: " + e.getMessage());
        }
    }

    @PutMapping("/update")
    public ApiResponse updateGoods(@RequestBody Goods goods) {
        try {
            goodsRepository.save(goods);
            return new ApiResponse(true, "商品更新成功");
        } catch (Exception e) {
            return new ApiResponse(false, "更新商品失败: " + e.getMessage());
        }
    }



    // 导入商品数据
    @PostMapping("/import")
    public ResponseEntity<List<Goods>> importGoods(@RequestParam("file") MultipartFile file) throws IOException {
        List<Goods> importedGoods = goodsService.importGoods(file);
        return ResponseEntity.ok(importedGoods);
    }
}
